#include <REGX52.H>
#include "Delay.h"

unsigned int MatrixKey()
{
	unsigned int keynumber = 0;
	
	P1 = 0xff;
	P1_7 = 0;
	if(P1_3 == 0){Delay(20);while(P1_3 == 0); Delay(20);keynumber = 1;}
	if(P1_2 == 0){Delay(20);while(P1_2 == 0); Delay(20);keynumber = 2;}
	if(P1_1 == 0){Delay(20);while(P1_1 == 0); Delay(20);keynumber = 3;}
	if(P1_0 == 0){Delay(20);while(P1_0 == 0); Delay(20);keynumber = 4;}

	P1 = 0xff;
	P1_6 = 0;
	if(P1_3 == 0){Delay(20);while(P1_3 == 0); Delay(20);keynumber = 5;}
	if(P1_2 == 0){Delay(20);while(P1_2 == 0); Delay(20);keynumber = 6;}
	if(P1_1 == 0){Delay(20);while(P1_1 == 0); Delay(20);keynumber = 7;}
	if(P1_0 == 0){Delay(20);while(P1_0 == 0); Delay(20);keynumber = 8;}

	P1 = 0xff;
	P1_5 = 0;
	if(P1_3 == 0){Delay(20);while(P1_3 == 0); Delay(20);keynumber = 9;}
	if(P1_2 == 0){Delay(20);while(P1_2 == 0); Delay(20);keynumber = 10;}
	if(P1_1 == 0){Delay(20);while(P1_1 == 0); Delay(20);keynumber = 11;}
	if(P1_0 == 0){Delay(20);while(P1_0 == 0); Delay(20);keynumber = 12;}

	P1 = 0xff;
	P1_4 = 0;
	if(P1_3 == 0){Delay(20);while(P1_3 == 0); Delay(20);keynumber = 13;}
	if(P1_2 == 0){Delay(20);while(P1_2 == 0); Delay(20);keynumber = 14;}
	if(P1_1 == 0){Delay(20);while(P1_1 == 0); Delay(20);keynumber = 15;}
	if(P1_0 == 0){Delay(20);while(P1_0 == 0); Delay(20);keynumber = 16;}
	
	return keynumber;
}